Skip to content

fix: correct cursor positioning with CJK (wide) characters#13

Open
WayneKent wants to merge 1 commit into
Tarquinen:mainfrom
WayneKent:fix/cjk-cursor-display-width
Open

fix: correct cursor positioning with CJK (wide) characters#13
WayneKent wants to merge 1 commit into
Tarquinen:mainfrom
WayneKent:fix/cjk-cursor-display-width

Conversation

@WayneKent

Copy link
Copy Markdown

Problem

After typing CJK characters and pressing ESC to enter normal mode,
pressing h/l to move the cursor causes it to jump to the middle of
the text (more noticeable with 4-5+ CJK chars). l sometimes cannot
reach the end of line.

Root Cause

input.cursorOffset returns display-width offset (CJK=2, ASCII=1),
but the code clamped it against text.length (JS char count).

Example: 5 CJK chars "你你你你你"
text.length = 5, displayWidth = 10
Cursor at end: cursorOffset = 10 (or 9)
clamp(..., 0, text.length) truncates 9/10 to 5
hostPosition maps offset 5 to the 3rd char -> jump to middle

Fix

Added width helpers (charDisplayWidth/charToDisplay/displayToChar/
displayWidth) in map.ts. All cursorOffset boundaries now use
displayWidth instead of text.length. Convert to char index only
when string manipulation is needed.

Files changed

  • src/modules/vim/map.ts
  • src/modules/vim/vimee.ts
  • src/modules/vim/index.tsx

Use display-width offsets instead of character indices when interacting
with input.cursorOffset, which returns display-width values. CJK chars
occupy 2 display columns but count as 1 JS character.

- Add charDisplayWidth/displayToChar/charToDisplay/displayWidth helpers
- Fix hostPosition/hostOffset/visualWrapOffsets in map.ts
- Fix clampNormalCursor, enterNormal, hostFromVimOffset, hostRange in vimee.ts
- Fix preparePassThroughKey, isNativeCompletionToken in index.tsx
@WayneKent

Copy link
Copy Markdown
Author

Hi, just a friendly bump — appreciate your time when you get a chance to review. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant